﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
using System.Text.Json.Serialization;

namespace ManagementPlatform.Utillity.SwaggerExpr
{
    public class SwaggerOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            //获取参数类型
            var type = context.ApiDescription.ActionDescriptor.Parameters.FirstOrDefault()?.ParameterType;

            var proty = type?.GetProperties() ?? new PropertyInfo[] { };

            foreach (var item in proty)
            {

                //查找忽略Attribute
                var attr = item.GetCustomAttribute<JsonIgnoreAttribute>();
                if (attr == null)
                    continue;

                //查找忽略参数
                var param = operation.Parameters.FirstOrDefault(p => p.Name == item.Name);
                if (param != null)
                    operation.Parameters.Remove(param);

            }
        }
    }
}
